<?php
/**
 * 配送员 ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysDistributionTechnician.class.php');

class ajax_distributionTechnician{

    public function execute() {

        if(isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])){

                switch($_REQUEST['operFlg']){

                    case 1://配送员列表
                        $this->distributionTechnicianList();
                        break;

                    case 2://查询配送员
                    $this->searchPayTypeDetail();
                    break;

                    case 100:
                        if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit','change'))) {

                            returnJsonData('参数错误');

                        }

                        if ($_REQUEST['act'] == 'edit') {

                            $this->edit();//新增、修改 配送员

                        } else if ($_REQUEST['act'] == 'del') {

                            $this->deleteDistributionTechnician();//删除配送员

                        } else if ($_REQUEST['act'] == 'change') {

                            $this->changeSendTypeUseflg(); //配送员启用/禁用

                        }

                        break;

                    default:

                        returnJsonData('参数错误');

                        break;
                }

        }else {
                returnJsonData('参数错误');
        }
    }

    /**
     * 配送员列表
     */
    private function distributionTechnicianList(){

        global $G_PGLOB;

        //实例化配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();
        $arg_array = $_REQUEST;
        $result = $sysDistributionTechnician->search( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($result) {

            $edit_permission = $G_PGLOB->has_permission(28);

            foreach($result as &$data) {

                if ($edit_permission) {

                    $operation_html = '';
                    $operation_html .= '<a href="javascript:void(0);" onclick="distribution.edit.open(' . $data['distribution_technician_id'] . ');">修改</a>&nbsp;';
                    $use_html = '';
                    if (isset($data['useFlg']) && $data['useFlg'] == 1) {
                        $use_html .= '<a href="javascript:void(0);" onclick="distribution.change.check(' . $data['distribution_technician_id'] . ', ' . $data['useFlg'] . ');">启用</a>';
                    } else {
                        $use_html .= '<a href="javascript:void(0);" onclick="distribution.change.check(' . $data['distribution_technician_id'] . ', ' . $data['useFlg'] . ');">禁用</a>';
                    }

                    $result_array['rows'][] = array (
                        'id' => $data['distribution_technician_id'],
                        'cell' => array(
                            $operation_html,
                            $data['distribution_technician_id'],
                            $data['distribution_technician_code'],
                            $data['distribution_technician_name'],
                            $data['mobile'],
                            //$data['distribution_name'],
                            $use_html
                        )
                    );

                } else {

                    $operation_html = '';
                    $result_array['rows'][] = array (
                        'id' => $data['distribution_technician_id'],
                        'cell' => array(
                            $operation_html,
                            $data['distribution_technician_id'],
                            $data['distribution_technician_code'],
                            $data['distribution_technician_name'],
                            $data['mobile'],
                            //$data['distribution_name'],
                            //$data['audio'],
                            $data['useFlg'] == 1 ? '启用' : '禁用',
                        )
                    );

                }

            }

        }


        echo json_encode($result_array);
    }

    /**
     * 新增/修改地址
     */
    private function edit() {

        global $G_SHOP;

        //实例配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        //新增/修改
        $arg_array = $_REQUEST;
        $arg_array['creator'] = $G_SHOP->currentUserID;
        $arg_array['distribution_technician_code'] = $arg_array['save_distribution_technician_code'];
        $arg_array['distribution_technician_name'] = $arg_array['save_distribution_technician_name'];
        $arg_array['mobile'] = $arg_array['save_mobile'];
        //$arg_array['distribution_id'] = $arg_array['save_distribution_id'];
        $result_array = $sysDistributionTechnician->editDistribution( $arg_array );

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 查询配送员方式
     */
    private function searchPayTypeDetail() {

        if (!isset($_REQUEST['distribution_technician_id']) || !quanli_is_int($_REQUEST['distribution_technician_id'])) {
            returnJsonData('参数错误');
        }

        //实例配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        //查询配送员方式
        $arg_array = array(
            'distribution_technician_id' => $_REQUEST['distribution_technician_id']
        );
        $distribution_technician_array = $sysDistributionTechnician->search( $arg_array );

        if ($distribution_technician_array) {
            returnJsonData('ok', 200, $distribution_technician_array[0]);
        } else {
            returnJsonData('配送员信息没有找到', 404);
        }

    }

    /**
     * 配送员启用/禁用
     */
    private function changeSendTypeUseflg () {

        //实例配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        //配送员启用/禁用
        $result_array = $sysDistributionTechnician->changeDistributionTechnicianUseflg( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 删除配送员
     */
    private function deleteDistributionTechnician () {

        //实例配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        //批量删除配送员
        $result_array = $sysDistributionTechnician->deleteDistributionTechnician( $_REQUEST['p_array'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

}
